<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Title</title>
    <link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../static/plugins/layui/css/formSelects-v4.css">
</head>
<style>
    .ok-body {
        padding: 10px
    }
</style>

<body>
<div class="ok-body">

    <blockquote class="layui-elem-quote layui-text">
        设备部位管理
    </blockquote>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <input type="hidden" name="id" id="id" th:value="${id}">
            <label class="layui-form-label">部位名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">必填</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">维护内容</label>
            <div class="layui-input-block" style="width: 600px">
                <textarea name="description" placeholder="请输入该部位的具体维护内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item" id="change" style="display: none">
            <label class="layui-form-label">是否修改绑定配件</label>
            <div class="layui-input-inline">
                <input type="checkbox" name="changeComponent" lay-skin="switch" value="1" lay-filter="change"
                       lay-text="是|否">
            </div>
        </div>

        <div class="layui-form-item" id="bindComponent">
            <label class="layui-form-label">绑定配件</label>
            <div class="layui-input-inline" style="width: 600px">
                <!--开启本地搜索-->
                <select name="components" lay-verify="required" xm-select="components" xm-select-show-count="50"
                        xm-select-search-type="dl" xm-select-skin="normal" xm-select-search="">
                    <option value=''>可搜索</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">必选</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="checkbox" checked="" name="status" lay-skin="switch" value="1" lay-filter="switchTest"
                       lay-text="启用|禁用">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>

<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/js/jquery-3.2.1.min.js"></script>
<script src="../static/js/jquery.base64.js"></script>
<script src="../static/js/pinyin.js"></script>
<script src="../static/js/device.js"></script>

<script th:inline="none">
    //全局定义一次, 加载formSelects
    layui.config({
        base: '../static/plugins/layui/lay/modules/' //此处路径请自行处理, 可以使用绝对路径
    }).extend({
        formSelects: 'formSelects-v4'
    });
    layui.use(['form', 'formSelects'], function () {
        var form = layui.form;
        var tableName = 'devicePart';
        var id = $('#id').val();
        var formSelects = layui.formSelects;

        //拼音搜索
        formSelects.filter('components', function (id, inputVal, val, isDisabled) {
            if (
                PY.fullPY(val.name).toLowerCase().indexOf(inputVal) != -1 ||    //拼音全拼是否包含
                PY.fullPY(val.name, true).indexOf(inputVal) != -1 ||            //拼音简拼是否包含
                val.name.indexOf(inputVal) != -1                                //文本是否包含
            ) {
                return false;
            }
            return true;
        });

        var firstCheckChange = true;
        //监听是否需要修改绑定配件(只在修改中使用)
        form.on('switch(change)', function (data) {
            if (data.elem.checked) {
                //如果需要修改则判断是否为第一次点击开关
                if (firstCheckChange) {
                    //如果是第一次则进行后台请求数据，并展示出来
                    JJZ.initMultipleSelect(formSelects, 'components', '/component/dataGrid', 'name', function (noError) {
                        if (noError) {
                            JJZ.requestData('/component/dataGridByPartId', 'partId=' + id, function (data) {
                                var result = data.records;
                                for (var index in result) {
                                    oldComponents.push(result[index].id);
                                }
                                formSelects.value('components', oldComponents);
                                firstCheckChange = false;
                                $('#bindComponent').css('display', 'block');
                            })
                        }
                    });
                } else {
                    //如果不是第一次则表示已经渲染过了，则不需要再从后台获取只要直接展示即可
                    $('#bindComponent').css('display', 'block');
                }
            } else {
                //如果不修改绑定配件信息则只需要不让其显示即可
                $('#bindComponent').css('display', 'none');
            }
        });

        var oldComponents = [];
        if (id) {
            //表示是修改页面
            $('#bindComponent').css('display', 'none');
            $('#change').css('display', 'block');
            var param = 'current=1&pageSize=2&searchCol=id&searchValue=' + id;
            JJZ.requestTableData(tableName, param, function (data) {
                var result = data.records[0];
                JJZ.named('name').val(result.name);
                JJZ.named('description').val(result.description);
                //绑定配件下拉框
                result.status == 1 ? '' : JJZ.named('status').removeAttr("checked");
                // JJZ.named('name').val(result.name);
            });

        } else {
            //表示是新增页面
            //绑定配件下拉框
            JJZ.initMultipleSelect(formSelects, 'components', '/component/dataGrid', 'name', function (noError) {
            });
        }

        form.on('submit(submit)', function (data) {
            data.field.status = data.field.status == 1 ? 1 : 0;
            delete  data.field.components;
            var newComponentList = formSelects.value('components', 'val');
            //降序排序
            newComponentList.sort(function (x, y) {
                return y - x;
            });
            if (id) {
                //如果为1则表示对设备配件信息进行了修改，则需要修改绑定配件数据
                data.field.editComponent = data.field.changeComponent == 1 ? true : false;
                data.field.oldComponentList = oldComponents;
                data.field.newComponentList = newComponentList;
                JJZ.editData(tableName, data.field, function (data) {
                    window.location.href = JJZ.baseUrl + '/' + tableName + '/devicePartDataGrid';
                })
            } else {
                data.field.newComponentList = newComponentList;
                JJZ.addData(tableName, data.field, function (data) {
                    window.location.href = JJZ.baseUrl + '/' + tableName + '/devicePartDataGrid';
                })
            }
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
</script>
</body>
</html>